﻿using System;
//using Microsoft.Practices.EnterpriseLibrary.Caching;
//using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
//using Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations;
//using Microsoft.Practices.EnterpriseLibrary.Caching.Instrumentation;
//using System.Web;
using System.Runtime.Caching;
using System.Linq;
using System.Collections.Generic;

//using Microsoft.Practices.EnterpriseLibrary.Caching;
//using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;


namespace _180FrameWork.Common
{
    public class CacheHelper
    {
        #region old
        //private static CacheManager _Cache = null;

        //static CacheHelper() {
        //    IBackingStore backStore = new NullBackingStore();
        //    ICachingInstrumentationProvider instrumentationProvider = new CachingInstrumentationProvider("MemoryCacheHelper", true, false, "MemoryCacheHelper");
        //    Cache cache = new Microsoft.Practices.EnterpriseLibrary.Caching.Cache(backStore, instrumentationProvider);
        //    ExpirationTask eT = new ExpirationTask(cache, instrumentationProvider);
        //    ScavengerTask sT = new ScavengerTask(10, 1000, cache, instrumentationProvider);
        //    BackgroundScheduler bS = new BackgroundScheduler(eT, sT, instrumentationProvider);
        //    ExpirationPollTimer ePT = new ExpirationPollTimer(60 * 1000);
        //    _Cache = new CacheManager(cache, bS, ePT);
        //}

        //public static CacheManager Cache
        //{
        //    get
        //    {
        //        return _Cache;
        //    }
        //}

        ///// <summary>
        ///// 缓存生存周期
        ///// </summary>
        //private static int absoluteTime = 5;

        ///// <summary>
        ///// 添加缓存
        ///// </summary>
        ///// <param name="cachename">缓存</param>
        ///// <param name="key">键</param>
        ///// <param name="value">值</param>
        ///// <param name="isRefresh">是否刷新</param>
        //public static void Add(string cachename, string key, object value)
        //{
        //    //ICacheManager cache = CacheFactory.GetCacheManager(cachename);
        //    //自定义刷新方式,如果过期将自动重新加载,过期时间为5分钟
        //    Cache.Add(key, value, CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromMinutes(absoluteTime)));
        //}


        ///// <summary>
        ///// 添加缓存
        ///// </summary>
        ///// <param name="cachename">缓存</param>
        ///// <param name="key">键</param>
        ///// <param name="value">值</param>
        ///// <param name="isRefresh">是否刷新</param>
        //public static void Add(string cachename, string key, object value, ICacheItemRefreshAction action)
        //{
        //    //ICacheManager cache = CacheFactory.GetCacheManager(cachename);
        //    if (action != null)
        //    {
        //        //自定义刷新方式,如果过期将自动重新加载,过期时间为5分钟
        //        Cache.Add(key, value, CacheItemPriority.Normal, action, new AbsoluteTime(TimeSpan.FromMinutes(absoluteTime)));
        //    }
        //    else
        //    {
        //        Cache.Add(key, value);
        //    }
        //}

        ///// <summary>
        ///// 获取缓存对象
        ///// </summary>
        ///// <param name="cachename">缓存</param>
        ///// <param name="key">键</param>
        ///// <returns></returns>
        //public static object GetCache(string cachename, string key)
        //{
        //    //ICacheManager cache = CacheFactory.GetCacheManager(cachename);
        //    return Cache.GetData(key);
        //}

        ///// <summary>
        ///// 移除缓存对象
        ///// </summary>
        ///// <param name="cachename">缓存</param>
        ///// <param name="key">键</param>
        //public static void RemoveCache(string cachename, string key)
        //{
        //    //ICacheManager cache = CacheFactory.GetCacheManager(cachename);
        //    Cache.Remove(key);
        //}
        ///// <summary>
        ///// 是否存在key
        ///// </summary>
        ///// <param name="cachename">缓存</param>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //public static bool Contains(string cachename, string key)
        //{
        //    //ICacheManager cache = CacheFactory.GetCacheManager(cachename);
        //    return Cache.Contains(key);
        //}

        #endregion
        /// <summary>
        /// 锁
        /// </summary>
        private static readonly object _locker = new object();
        private static MemoryCache _cache = MemoryCache.Default;
        /// <summary>
        /// 是否存在Cache元素
        /// </summary>
        /// <param name="key">元素的键</param>
        /// <returns></returns>
        public static bool Contains(string key)
        {
            return _cache.Contains(key);
        }
        /// <summary>
        /// 添加Catch元素
        /// </summary>
        /// <param name="key">元素的键</param>
        /// <param name="value">元素的值</param>
        /// <param name="slidingExpiration">元素过期时间(时间间隔)</param>
        /// <param name="absoluteExpiration">元素过期时间(绝对时间)</param>
        public static void Set(string key, object value, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
        {
            lock (_locker)
                _cache.Set(key, value, CreatePolicy(slidingExpiration, absoluteExpiration));
        }
        /// <summary>
        /// 获取Catch元素
        /// </summary>
        /// <param name="key">元素的键</param>
        /// <returns></returns>
        public static object Get(string key)
        {
            if (_cache.Contains(key))
                return _cache[key];
            else
                return null;
        }
        /// <summary>
        /// 移出Cache元素 
        /// </summary>
        /// <param name="key">待移除元素的键</param>
        /// <returns></returns>
        public static object Remove(string key) {
            if (_cache.Contains(key))
            {
                lock (_locker)
                    return _cache.Remove(key);
            }
            else
                return null;
        }

        /// <summary>
        /// 设置过期信息
        /// </summary>
        /// <param name="slidingExpiration"></param>
        /// <param name="absoluteExpiration"></param>
        /// <returns></returns>
        private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
        {
            var policy = new CacheItemPolicy();

            if (absoluteExpiration.HasValue)
            {
                policy.AbsoluteExpiration = absoluteExpiration.Value;
            }
            else if (slidingExpiration.HasValue)
            {
                policy.SlidingExpiration = slidingExpiration.Value;
            }

            policy.Priority = CacheItemPriority.Default;

            return policy;
        }

    }
}
